foreach y in coholder {

	local category "female median_total_income_quart age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers benefits_pers inv_ben_pers median_cons2_terc median_cons1_terc"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1
	foreach var in `category' {
		if "`var'" == "female" | "`var'" == "tlottery_pers" | "`var'" == "tlottery_winner" | "`var'" == "loan_wo_pers" | "`var'" == "gambling_pers" | "`var'" == "benefits_pers" | "`var'" == "inv_ben_pers" {
			local min 0
			local max 1
		} 
		else if "`var'" == "median_total_income_quart" | "`var'" == "age_cat" {
			local min 1
			local max 4
		}
		else if "`var'" == "median_cons2_terc" | "`var'" == "median_cons1_terc" {
			local min 1
			local max 3
		}
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i' 
			ci means `y' if `var' == `j' & edate_id==1335, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	 rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 title("Probability of being a coholder", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 mcolor(blue) lcolor(blue) mstyle(p1) ///
	 xline(3 8 13 16 19 22 25 28 31 35, lpattern(dot)) ///
	 xlabel(1 "Male" 2 "Female" ///
		 4 "Income: Low" 5 "Lower-middle" 6 "High-middle" 7 "High" ///
		 9  "Age: <30" 10 "30-45" 11 "45-60" 12 "60+" ///
		 14 "No lottery" 15 "Lottery" ///
		 17 "No lottery winner" 18 "Lottery winner" ///
		 20 "No loan write-offs" 21 "Loan write-offs" ///
		 23 "No gambler" 24 "Gambler" ///
		 26 "No benefits" 27 "Benefits" ///
		 29 "No inv. benefits" 30 "Inv. benefits" ///
		 32 "Liquidity: Low" 33 "Medium" 34 "High" ///
		 36 "Cash: Low" 37 "Medium" 38 "High", ///
		 labsize(medium) angle(90))  ///
	 ylabel(, labsize(medium) angle(0) format(%9.1fc)) /// 
	 xscale(range(0.25 38.75)) ///
	 legend(off)   
}
drop subcategory mean lqt uqt n 
drop min max  	 

graph export "Figures/CohDescr_WhoCoholds1.png", width(1800) height(1300) replace
graph export "Figures/CohDescr_WhoCoholds1.pdf", replace



foreach y in cohold_days_share {

	local category "female median_total_income_quart age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers benefits_pers inv_ben_pers median_cons2_terc median_cons1_terc"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1
	foreach var in `category' {
		if "`var'" == "female" | "`var'" == "tlottery_pers" | "`var'" == "tlottery_winner" | "`var'" == "loan_wo_pers" | "`var'" == "gambling_pers" | "`var'" == "benefits_pers" | "`var'" == "inv_ben_pers" {
			local min 0
			local max 1
		} 
		else if "`var'" == "median_total_income_quart" | "`var'" == "age_cat" {
			local min 1
			local max 4
		}
		else if "`var'" == "median_cons2_terc" | "`var'" == "median_cons1_terc" {
			local min 1
			local max 3
		}
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i' 
			ci means `y' if `var' == `j' & edate_id==1335 & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 title("Share of days coholding (for coholders)", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("") ///
	 mcolor(blue) lcolor(blue) mstyle(p1) ///
	  xline(3 8 13 16 19 22 25 28 31 35, lpattern(dot)) ///
	   xlabel(1 "Male" 2 "Female" ///
		 4 "Income: Low" 5 "Lower-middle" 6 "High-middle" 7 "High" ///
		 9  "Age: <30" 10 "30-45" 11 "45-60" 12 "60+" ///
		 14 "No lottery" 15 "Lottery" ///
		 17 "No lottery winner" 18 "Lottery winner" ///
		 20 "No loan write-offs" 21 "Loan write-offs" ///
		 23 "No gambler" 24 "Gambler" ///
		 26 "No benefits" 27 "Benefits" ///
		 29 "No inv. benefits" 30 "Inv. benefits" ///
		 32 "Liquidity: Low" 33 "Medium" 34 "High" ///
		 36 "Cash: Low" 37 "Medium" 38 "High", ///
		 labsize(medium) angle(90))  ///
	 ylabel(, labsize(medium) angle(0) format(%9.1fc)) /// 
	 xscale(range(0.25 38.75)) ///
	 legend(off)   
}
drop subcategory mean lqt uqt n 
drop min max  	 

graph export "Figures/CohDescr_WhoCoholds2.png", width(1800) height(1300) replace
graph export "Figures/CohDescr_WhoCoholds2.pdf", replace



********************************************************************************


*T-test: 
foreach var in coholder {

if `var' == coholder {
local mtitle "No coholder" "Coholder" ""
}

eststo a: quietly estpost summarize ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==0 & edate_id==1335, detail

eststo b: quietly estpost summarize ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==1 & edate_id==1335, detail

eststo diff: quietly estpost ttest ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==1 & edate_id==1335| `var'==0 & edate_id==1335, by(`var')

esttab a b diff using "Tables/Coh_DescrWhoCoholds_`var'.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 0 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("`mtitle'") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
female "Female" median_total_income_quart "Total income quartile" ///
age_cat "Age category" tlottery_pers "Lottery person" ///
tlottery_winner "Lottery winner" loan_wo_pers "Loan write-off person" ///
gambling_pers "Gambler" benefits_pers "Benefits person" inv_ben_pers "Inv! benefit person" ///
median_cons2_terc "Liquidity terc. (cons. days)" ///
median_cons1_terc "Cash terc. (cons. days)" ///
) ///
substitute(? \ ! .)
}

foreach var in frequent_coholder {

if `var' == frequent_coholder {
local mtitle "No frequent coholder" "Frequent coholder" ""
}
eststo a: quietly estpost summarize ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==0 & edate_id==1335 & coholder==1, detail

eststo b: quietly estpost summarize ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==1 & edate_id==1335 & coholder==1, detail

eststo diff: quietly estpost ttest ///
female median_total_income_quart ///
age_cat tlottery_pers tlottery_winner loan_wo_pers gambling_pers ///
benefits_pers inv_ben_pers ///
median_cons2_terc median_cons1_terc ///
if `var'==1 & edate_id==1335 & coholder==1 | `var'==0 & edate_id==1335 & coholder==1, by(`var')

esttab a b diff using "Tables/Coh_DescrWhoCoholds_`var'.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 0 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("`mtitle'") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
female "Female" median_total_income_quart "Total income quartile" ///
age_cat "Age category" tlottery_pers "Lottery person" ///
tlottery_winner "Lottery winner" loan_wo_pers "Loan write-off person" ///
gambling_pers "Gambler" benefits_pers "Benefits person" inv_ben_pers "Inv! benefits person" ///
median_cons2_terc "Liquidity terc. (cons. days)" ///
median_cons1_terc "Cash terc. (cons. days)" ///
) ///
substitute(? \ ! .)
}





